﻿
using System;
using System.Web;

namespace Log.IO
{

    /// <summary>
    /// 系统的固定数据，比如网站根路径、app路径、主机名称等
    /// </summary>
    public class SystemInfo {

        public static SystemInfo Instance = loadSystemInfo();

        private SystemInfo() { }

        public static String RootPath { get { return SystemInfo.Instance.rootPath; } }
        public static String ApplicationPath { get { return SystemInfo.Instance.applicationPath; } }
        public static String Host { get { return SystemInfo.Instance.host; } }

        private String rootPath;
        private String applicationPath;
        private String host;

        //-------------------------------------------------------------------------------------------------


        private static bool _hasInitialized = false;
        private static Object _objLock = new Object();

        public static void Init() {

            if (_hasInitialized) return;

            lock (_objLock) {
                if (_hasInitialized) return;
                SystemInfo obj = SystemInfo.Instance;
                _hasInitialized = true;
            }
        }


        private static SystemInfo loadSystemInfo() {

            SystemInfo obj = new SystemInfo();

            if (IsWeb) {
                obj.applicationPath = HttpContext.Current.Request.ApplicationPath;
                obj.rootPath = addEndSlash( obj.applicationPath );
                obj.host = HttpContext.Current.Request.Url.Host;
            }
            else {
                obj.applicationPath = "/";
                obj.rootPath = "/";
                obj.host = "localhost";
            }

            return obj;

        }

        private static String addEndSlash( String appPath ) {
            if (!appPath.EndsWith( "/" )) return appPath + "/";
            return appPath;
        }


        public static Boolean IsWeb { get { return HttpContext.Current != null; } }
        public static Boolean IsWindows {
            get { return Environment.OSVersion.VersionString.ToLower().IndexOf( "windows" ) >= 0; }
        }


    }

}
